﻿using AutoMapper;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warehouse.Application.Dto.Input;
using Warehouse.Application.Dto.Output;
using Warehouse.Application.Services.Warehouse;
using Warehouse.Domain.Entities;
using Warehouse.Infrastructure.Repositories;
using Warehouse.Result;
using Yitter.IdGenerator;

namespace Warehouse.Application.Services.Externallabl
{
    public class ExternallabelModelAppService:IExternallabelModelAppService
    {
        private readonly IWarehouseModelRepository<ExternallabelModel> _externallabelModelRepository;
        private readonly IMapper _mapper;
        private readonly IBasicModelRepository<SupplierModel> _supplierModelRepository;

        public ExternallabelModelAppService(IWarehouseModelRepository<ExternallabelModel> externallabelModelRepository, IMapper mapper, IBasicModelRepository<SupplierModel> supplierModelRepository)
        {
            _externallabelModelRepository = externallabelModelRepository;
            _mapper = mapper;
            _supplierModelRepository = supplierModelRepository;
        }


        /// <summary>
        /// 创建外部标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public  ApiResult<int> CreateExternallabel(CreateExternallabelInput input)
        {
            var model = _mapper.Map<ExternallabelModel>(input);
            model.Id = YitIdHelper.NextId();
            model.CreatedAt = DateTime.Now;
            model.IsDeleted = false; 
            model.Resolution=model.Formats.Split(",").Length;
            var list = _externallabelModelRepository.GetValue().FirstOrDefault(x=>x.SupplierId==model.SupplierId &&x.IsDeleted==false);
            if (list != null)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnum.Fail,
                    Message = "该供应商已存在外部标签"
                };
            }


            _externallabelModelRepository.Create(model);
            return new ApiResult<int>()
            {
                Code = ApiEnum.Ok,
                Message = "添加外部标签成功",
                Data = 1
            };
        }



        /// <summary>
        /// 获取外部标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiPaing<ExternallabelOutput> GetExternallabel(PageInput input)
        {
            var externallabel = _externallabelModelRepository.GetValue().Where(x => x.IsDeleted == false);
            var supplier = _supplierModelRepository.GetValue().Where(x => x.IsDeleted == false);
            var linq = from a in externallabel
                       join
                      b in supplier on a.SupplierId equals b.Id
                       select new ExternallabelOutput()
                       {
                           Id = a.Id,
                           ExternalLabelName = a.ExternalLabelName,
                           SupplierId = a.SupplierId,
                           SupplierName = b.SupplierName,
                           Format = a.Formats,
                           Separator = a.Separator,
                           Resolution = a.Resolution,
                           Start = a.Statust,
                           ExternalLabelDescribe = a.ExternalLabelDescribe
                       };
            var list = linq.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            return new ApiPaing<ExternallabelOutput>
            {
                TotalCount = list.Count(),
                PageDatas = list,
                Code = ApiEnum.Ok,
                Message = "查询成功"
            };

        }


        /// <summary>
        /// 获取一条外部标签
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ApiResult<ExternallabelModel> GetModelExternallabel(long Id)
        {
            var list =_externallabelModelRepository.GetValue().Where(x => x.Id == Id).FirstOrDefault();
            return new ApiResult<ExternallabelModel>
            {
                Code = ApiEnum.Ok,
                Message = "查询成功",
                Data = list
            };
        }

        /// <summary>
        /// 删除外部标签
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteExternallabel(long Id)
        {
            var list =_externallabelModelRepository.GetValue().FirstOrDefault(x => x.Id == Id);
            if (list == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.Fail,
                    Message = "未找到数据"
                };
            }
            list.IsDeleted = true;
            _externallabelModelRepository.Update(list);
            return new ApiResult<int>
            {
                Code = ApiEnum.Ok,
                Message = "删除成功",
                Data = 1
            };
        }

        /// <summary>
        /// 创建外部标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateExternallabel(UpdateExternallabelInput input)
        {
            var list = _externallabelModelRepository.GetValue().FirstOrDefault(x => x.Id == input.Id&& x.IsDeleted==false);
            if (list == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.Fail,
                    Message = "未找到该标签"
                };
            }
            var model = _mapper.Map(input, list);
            model.UpdatedAt = DateTime.Now;
            _externallabelModelRepository.Update(model);
            return new ApiResult<int>
            {
                Code = ApiEnum.Ok,
                Message = "修改成功",
                Data = 1
            };
        }


    }
}
